/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */
package info.peigen.hotpot.common.service.base;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson2.annotation.JSONField;
import com.google.common.collect.Maps;
import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.core.result.ResultBase;
import info.peigen.hotpot.component.data.repository.Repository;
import info.peigen.hotpot.component.data.repository.beetlsql.util.BeetlSqlPageResultUtil;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import org.beetl.sql.mapper.BaseMapper;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * <b>(ServiceQueryContext)</b>
 * 查询服务的上下文
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2019/05/18
 */
@SuppressWarnings("rawtypes")
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class ServiceQueryContext<ORDER extends ServiceOrderBase, RESULT extends ResultBase, ENTITY, INFO, MAPPER extends BaseMapper<ENTITY>, REPOSITORY extends Repository> implements Serializable {
    static final long serialVersionUID = -7996169895294878828L;
    /** 当前数据库时间戳 <code>currentTimestamp</code> */
    Timestamp          currentTimestamp;
    /** 请求参数 <code>order</code> */
    ORDER              order;
    /** 应答返回 <code>result</code> */
    RESULT             result;
    /** repositoryService */
    REPOSITORY         repository;
    /** Mapper */
    MAPPER             mapper;
    /** entity */
    ENTITY             entity;
    /** 批量entity */
    List<ENTITY>       entities;
    /** 分页entity */
    PageResult<ENTITY> page;
    /** 查询结果的Info */
    INFO               info;
    /** 扩展参数 */
    @Builder.Default
    Map<String, Object> extend = Maps.newHashMap();

    @Builder.Default
    @JSONField(serialize = false, deserialize = false)
    StopWatch watch = new StopWatch();

    public void wrapPage(org.beetl.sql.core.page.PageResult pageResult) {
        this.setPage(BeetlSqlPageResultUtil.beetlSqlPageResultFromSuccess(pageResult));
        BeanUtil.copyProperties(pageResult, this.getPage());
    }

}